# test non-event objects and methods
use PlPN qw(PN PNSK_INTERFACE TRUE FALSE OTabWidth);

use PlPN;

#
# basic PN functions
#

my $pn = PN;
my $ifv = PN->GetIFaceVersion;
my $pnv = PN->GetVersion;
my $handle = PN->GetMainWindow;

my $input = PN->InputBox("Test Title", "Please enter some input");

PlPN->alert(<<INFO);
Basic PlPN::PN functions:

PN: $pn
Interface version: $ifv
PN version: $pnv
Main window handle: $handle
User input: $input
INFO

#
# Options Manager
#

my $om = PN->GetOptionsManager;

my $ami = $om->Get(PNSK_INTERFACE, 'AllowMultiInstance', TRUE);
my $mru = $om->Get(PNSK_INTERFACE, 'MRUSize', 1);
my $lcs = $om->Get(PNSK_INTERFACE, 'LastClipsScheme', 'none');

$om->Set('PlPN', 'TestBool', FALSE);
$om->Set('PlPN', 'TestInt', 0);
$om->Set('PlPN', 'TestStr', 'none');

$om->BeginGroupOperation('PlPN');

$om->Set('TestBool', TRUE);
$om->Set('TestInt', 255);
$om->Set('TestStr', 'PlPN');

my $tb = $om->Get('TestBool', TRUE);
my $ti = $om->Get('TestInt', 0);
my $ts = $om->Get('TestStr', '');

$om->EndGroupOperation();

my $tw = $om->GetCached(OTabWidth);
$om->SetCached(OTabWidth, $tw);

PlPN->alert(<<INFO);
PlPN::Options functions:

Options: $om
AllowMultiInstance: $ami
MRUSize: $mru
LastClipsScheme: $lcs
Test Bool: $tb
Test Int: $ti
Test Str: $ts
Tab Width: $tw
INFO

#
# Document
#

my $opened = PN->OpenDocument('PlPN.pm', 'perl');
my $current = PN->GetCurrentDocument();

my $title = $opened->GetTitle;
my $file = $opened->GetFileName;
my $scheme = $opened->GetCurrentScheme;
my $sci = $opened->GetScintillaHWND;
my $mod = $opened->GetModified;
my $wp = $opened->GetWriteProtect;
my $cs = $opened->GetCanSave;

# TODO: what is a valid document?
# seems to mean it has a document window associated with it

my $valid = $opened->IsValid;

$opened->Activate;
$opened->Close(1);	# 1 to force close without prompting

PlPN->alert(<<INFO);
PlPN::Document functions:

Title: $title
File: $file
Scheme: $scheme
Scintilla: $sci
Modified: $mod
Write Protect: $wp
Can Save: $cs
Valid: $valid
INFO

#
# OutputWindow
#

my $gow = PN->GetGlobalOutputWindow();
my $msg = "filename: PlPN.pm line: 135\n";

# in Perl we can ignore the length; if XS we can use the SV length

$gow->AddToolOutput($msg, length($msg));
$gow->SetToolBasePath(PN->GetOptionsManager()->GetPNPath);

# TODO: presumably, bBuiltin true means use the builtin parser 
# if bBuiltin is true OR customExpression is NULL, it will use the builtin output parser
# if bBuiltin is false AND customeExpression is not NULL, it will use customExpression
# in other words, if customExpression is NULL, it will ignore the value of bBuiltin and just use the builtin parser
# probably in Perl we should just pass in a false value to set the builtin parser, and a true value to set the custom expression

$gow->SetToolParser("filename: (.+?) line: (.+?)");

$gow->ShowOutput;

PlPN->alert("Stop so we can see the output window");

$gow->ClearOutput;
$gow->HideOutput;

#
# SearchOptions
#

my $so = $om->GetSearchOptions;
my $uso = PN->GetUserSearchOptions;

my $ftx = $so->FindText('->');
my $mww = $so->MatchWholeWord(0);
my $mc = $so->MatchCase(0);
my $urx = $so->UseRegExp(0);
my $ftg = $so->FindTarget(PlPN::elwCurrentDoc);
my $lok = $so->LoopOK(1);
my $us = $so->UseSlashes(1);
my $ncm = $so->NoCursorMove(0);
my $rt = $so->ReplaceText('->');
my $ris = $so->ReplaceInSelection(0);
my $fx = $so->FileExts('.*');
my $sp = $so->SearchPath('/');
my $rc = $so->Recurse(1);
my $ih = $so->IncludeHidden(1);
my $fs = $so->FileSet(PlPN::fifSingleFile);
my $fnd = $so->Found(0);

my $opened = PN->OpenDocument('PlPN.pm', 'perl');
PN->FindInFiles($so);
my $fn = $opened->FindNext($so);
my $rp = $opened->Replace($so);
my $ra = $opened->ReplaceAll($so);
$opened->Close(1);	# close without asking to save

PlPN->alert(<<INFO);
PlPN::SearchOptions functions:

FindText: $ftx
MatchWholeWord: $mww
MatchCase: $mc
UseRegExp: $urx
FindTarget: $ftg
LoopOK: $lok
UseSlashes: $us
NoCursorMove: $ncm
ReplaceText: $rt
ReplaceInSelection: $ris
FileExts: $fx
SearchPath: $sp
Recurse: $rc
IncludeHidden: $ih
FileSet: $fs
Found: $fnd

FindNext: $fn
Replace: $rp
ReplaceAll: $ra
INFO

#
# overloaded comparison operator
#

my $pobj1 = PN->GetGlobalOutputWindow;
my $pobj2 = PN->GetGlobalOutputWindow;
my $equal = ($pobj1 == $pobj2) ? 'true' : 'false';

PlPN->alert(<<INFO);
Overloaded comparisons:

Perl object 1: $pobj1 ($pobj1->{_cpp_ptr})
Perl object 2: $pobj2 ($pobj1->{_cpp_ptr})
Equal: $equal
INFO
